#include "sensor.h"

Sensor::Sensor()
{

}

// 解码传感器返回信息
void Sensor::decodeResponse(QByteArray buf)
{
    int i = buf.length();
    if(i > 3 && buf.at(0) == '!')
    {
        switch (buf.at(1))
        {
        case 'M':   // 单、双色工作模式。
            display_mode_mr = buf.at(2);
            break;
        case 'E':   // 发射率，1C模式需要设置。0.10..1.00; ?E; E=0.95; !E0.95
            emissisity = buf.trimmed().right(4);
            break;
        case 'S':   // 坡度，2C模式需要设置。0.850..1.150; ?S; S=0.850; !S0.850
            slope = buf.trimmed().right(5);
            break;
        case 'U':   // 温度单位（摄氏/华氏）。C OR F; ？U; U=C; !UC
            temp_units = buf.at(2);
            break;
        case 'P':   // 峰值保持时间。nnn.n  0..299.9; ?P; P=005.6; !P005.6
            peak_hold_time = buf.trimmed().right(5);
            break;
        case 'V':   // 传输模式B/P  V=P !VP
            poll_burst_mode = buf.at(2);
            break;
        case 'Z':   // 衰减。nn 0-99%;  ?Z; Z=95; !Z95
            attenaution = buf.trimmed().right(2);
            break;
        case 'X':
            switch (buf.at(2))
            {
            case 'R':   // 修订版本

                break;
            case 'U':   // 传感器型号
                identify_unit = buf.mid(3).trimmed();
                break;
            case 'V':   // 传感器序列号
                sensor_sn = buf.mid(3).trimmed();
                break;
            case 'F':   // 出厂模式

                break;
            }
            break;
        case 'T':   // 温度数值
        case 'W':
        case 'N':
            temperature = buf.trimmed().right(4);
            break;
        }
    }

    if(poll_burst_mode == "B" && buf.at(0) == 'C') //  C TEAAA S1.000 I048
    {
        QList<QByteArray> sensorByteArrays = buf.split(' ');
        temp_units  = QString(sensorByteArrays.value(0));
        temperature = QString(sensorByteArrays.value(1).right(4));
        slope       = QString(sensorByteArrays.value(2).right(5));
        internal_ambient = QString(sensorByteArrays.value(3).right(3));
    }
}

// 单、双色工作模式。1 OR 2; ?M; M=1; !M1
QString Sensor::set_display_mode_mr(int mode)
{
    QString r;
    if(mode == 1 || mode == 2)
    {
        display_mode_mr = QString::number(mode);
        r = "M=" + display_mode_mr + "\r";
    }
    return r;
}

// 发射率，1C模式需要设置。0.10..1.00; ?E; E=0.95; !E0.95
QString Sensor::set_emissisity(double emis)
{
    QString r;
    if(emis >= 0.1 && emis <= 1.0)
    {
        emissisity = QString("%1").arg(emis, 4, 'f', 2, QChar('0'));
        r = "E=" + emissisity + "\r";
    }
    return r;
}

// 坡度，2C模式需要设置。0.850..1.150; ?S; S=0.850; !S0.850
QString Sensor::set_slope(double slope)
{
    QString r;
    if(slope >= 0.10 and slope <= 1.00)
    {
        emissisity = QString("%1").arg(slope, 5, 'f', 3, QChar('0'));
        r = "S=" + emissisity + "\r";
    }
    return r;
}

// 温度单位（摄氏/华氏）。C OR F; ？U; U=C; !UC
QString Sensor::set_temp_units(QString units)
{
    QString r;
    if(units == "C" || units == "F")
    {
        temp_units = units;
        r = "U=" + temp_units + "\r";
    }
    return r;
}

// 峰值保持时间。nnn.n  0..299.9; ?P; P=005.6; !P005.6
QString Sensor::set_peak_hold_time(double pkhtime)
{
    QString r;
    if(pkhtime >= 0 and pkhtime <= 299.9)
    {
        peak_hold_time = QString("%1").arg(pkhtime, 5, 'f', 1, QChar('0'));
        r = "P=" + peak_hold_time + "\r";
    }
    return r;
}

// 传输模式B/P  V=P !VP
QString Sensor::set_poll_burst_mode(QString mode)
{
    QString r;
    if(mode == "B" || mode == "P")
    {
        temp_units = mode;
        r = "V=" + temp_units + "\r";
    }
    return r;
}

// 衰减。nn 0-99%;  ?Z; Z=95; !Z95
QString Sensor::set_attenaution(int atten)
{
    QString r;
    if(atten >= 0 and atten <= 299.9)
    {
        attenaution = QString("%1").arg(atten, 2, 10, QChar('0'));
        r = "Z=" + attenaution + "\r";
    }
    return r;
}

// 恢复工厂设置
QString Sensor::restore_factory_defaults()
{
    return "XF\r";
}

QString Sensor::get_display_mode_mr()
{
    return display_mode_mr;
}

QString Sensor::get_emissisity()
{
    return emissisity;
}

QString Sensor::get_slope()
{
    return slope;
}

QString Sensor::get_temp_units()
{
    return temp_units;
}

QString Sensor::get_peak_hold_time()
{
    return peak_hold_time;
}

QString Sensor::get_poll_burst_mode()
{
    return poll_burst_mode;
}

QString Sensor::get_identify_unit()
{
    return identify_unit;
}

QString Sensor::get_sensor_sn()
{
    return sensor_sn;
}

QString Sensor::get_attenaution()
{
    return attenaution;
}

QString Sensor::get_temperature()
{
    return temperature;
}

QString Sensor::get_internal_ambient()
{
    return internal_ambient;
}
